// pages/user/updateInfo/updateInfo.ts
import User, { UserInfo } from "../../../utils/user";
import { getGradeClasses, updateAdminInfo } from "../../../utils/request/api";
import msg from "../../../utils/msg";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{} as UserInfo,
    adminInfo:{password:'',account:''} as {password:string,account:string},
    classes : [] as Array<{text:string,value:string|number,children:Array<{text:string,value:string | number}>}>,
    columns: [
      {
        values: [] as Array<string>,
        className: 'column1',
      },
      {
        values: [] as Array<string>,
        className: 'column2',
        defaultIndex: 0,
      },
    ],
    show: false,
    showAdmin :false,
  },
  //提交
  submit:async function(){
    let user = new User;
    let data = this.data.userInfo;
    data['openid']=user.openid;
    if(user.code){
      msg.show('该微信号已经绑定学号为:'+user.code+'的学生信息','更换绑定学生请联系管理员重置');
      return;
    }
    await msg.confirm('一个微信号只能绑定一位学生信息,解绑定需要管理员操作','是否继续绑定');
    msg.Loading('上传中');
    await user.updateInfo(data)
    .then((code:any)=>{ 
      msg.hideLoading();
      if(code==200){
        setTimeout(function(){
          wx.navigateBack();
        },1500)
      }
    });
  },
  //后台账号
  bindAdminSubmit:async function(){
    let user2 = new User;
    if(user2.isAdmin){
      msg.show('该微信号已经绑定了一个辅导员或学生工作者账号','如要更改请联系管理员重置');
      return;
    }
    await msg.confirm('一个微信号只能绑定一个账号','是否继续绑定');
    msg.Loading('上传中');
    await updateAdminInfo({password:this.data.adminInfo.password,account:this.data.adminInfo.account,openid:this.data.userInfo.openid})
    .then((res:any)=>{
      msg.hideLoading();
      user2.setAtt(res.data.data);
      user2.setStorage();
      if(res.data.code==200){
        msg.show('登陆成功','');
        setTimeout(function(){
          wx.navigateBack();
        },1500)
      }
      else{
        msg.show(res.data.data,'');
      }

    })
  },
  //获取输入
  getInput(e:any){
    let index = e.currentTarget.dataset.index;
    this.setData({
      ['userInfo.'+index]:e.detail.value,
    });
  },
  //多级选择完成
  onFinish(e:any){
    let class_id=e.detail.value;
    let className = e.detail.selectedOptions[1].text;
    let userInfo = this.data.userInfo;
    userInfo.className=className;
    userInfo.class_id=parseInt(class_id);
    this.setData({
      userInfo:userInfo,
    });
  },
  onClose(){
    this.setData({
      show:false,
      showAdmin:false,
    })
  },
  chooseClass(){
    this.setData({
      show:true,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:async function(option:{openid:string}) {
    let user = new User;
    let self = this;
    if(option.openid){
      user.openid=option.openid;
    }
    this.setData({
      userInfo:user.getInfo(),
    })
    //拿取年级-班级数据
    await getGradeClasses()
    .then((res:any)=>{
      self.setData({
        classes:res.data.data,
      })
    });
  },
  onClickLeft(){
    wx.navigateBack();
  },
  bindAdmin(){
    this.setData({
      showAdmin:true,
    })
  },
  getAccount(e:any){
    this.setData({
      ['adminInfo.account']:e.detail.value,
    })
  },
  getPassword(e:any){
    this.setData({
      ['adminInfo.password']:e.detail.value,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },  

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})